/**
 * BaseIndexingTestCase.java 1.0 2007-6-9 上午01:08:45,创建 by yangfan
 */


package lia.ch1;


import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-9 上午01:08:45,创建
 */
public class BaseIndexingTestCase
{
	protected String[] keywords = {"1","2","3"};
	protected String[] unindexed = {"Netherlands","Italy","China",};
	protected String[] unstored = {"Amsterdam has lots of bridges","Venice has lots of canals","Venice has lots of canals",};
	protected String[] text = {"Amsterdam","Venice","Venice",};
	protected Directory dir;

	@Before
	public void setUp() throws Exception
	{
		System.out.println("file.sparator==>" + File.separator );
		System.out.println("file.pathSparator==>" + File.pathSeparator );
		String indexDir = System.getProperty("java.io.tmpdir","tmp") + File.separator + "index-dir";
		dir = FSDirectory.getDirectory(indexDir, true);
		addDocuments(dir);
	}

	protected void addDocuments(Directory dir2) throws IOException
	{
		IndexWriter writer = new IndexWriter(dir,getAnalyzer(),true);
		writer.setUseCompoundFile(isCompound());

		for(int i = 0; i < keywords.length; i++)
		{
			Document doc = new Document();
			doc.add(new Field("id",keywords[i],Field.Store.YES,Field.Index.UN_TOKENIZED));
			doc.add(new Field("country",unindexed[i],Field.Store.YES,Field.Index.NO));
			doc.add(new Field("contents",unstored[i],Field.Store.NO,Field.Index.TOKENIZED));
			doc.add(new Field("city",text[i],Field.Store.YES,Field.Index.UN_TOKENIZED));
			writer.addDocument(doc);
		}

		writer.optimize();
		writer.close();
	}

	protected boolean isCompound()
	{
		return true;
	}

	protected Analyzer getAnalyzer()
	{
		return new SimpleAnalyzer();
	}
}
